//
//  RecordHomeViewController.m
//  TourPlay
//
//  Created by dllo on 15/11/17.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "RecordHomeViewController.h"
#import "ActivitieDetailsViewController.h"
#import "RecordHomeCell.h"

@interface RecordHomeViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *recordTable;
@property (nonatomic, retain) MBProgressHUD *hub;
@property (nonatomic, retain) NSMutableArray *recordArr;

@end

@implementation RecordHomeViewController
- (void)dealloc
{
    [_hub release];
    [_recordTable release];
    [_recordArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.recordArr = [NSMutableArray array];

    self.recordTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 64) style:UITableViewStylePlain];
    self.recordTable.delegate = self;
    self.recordTable.dataSource = self;
    self.recordTable.backgroundColor = [UIColor clearColor];
    [self.recordTable registerClass:[RecordHomeCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.recordTable];
    [_recordTable release];
    
    self.recordTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    

    
    // 接收城市改变通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(homeRefurbish:) name:@"homeRefurbish" object:nil];
 
    [self addHeaderRefresh];
}
// 城市改变调用
- (void)homeRefurbish:(NSNotification *)sender
{
    [self.recordTable.header beginRefreshing];
 
}
-(void)addHeaderRefresh
{
    self.recordTable.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        [self getUpdate];
        
    }];
    [self.recordTable.header beginRefreshing];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecordMod *recordMod = [self.recordArr objectAtIndex:indexPath.row];

    
    return [RecordHomeCell getRecordViewHeight:recordMod];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.recordArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"cell";
    RecordHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    cell.recordMod = [self.recordArr objectAtIndex:indexPath.row];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.backgroundColor = [UIColor clearColor];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    ActivitieDetailsViewController *activitieVC = [[ActivitieDetailsViewController alloc] init];
    RecordMod *mod = [self.recordArr objectAtIndex:indexPath.row];
    activitieVC.activityId = mod.unionId;
    activitieVC.activityTitle = mod.projectName;
    // 隐藏
    activitieVC.hidesBottomBarWhenPushed = YES;
    [self.delegate navigationViewWithIsHidden:YES];
    [self.navigationController pushViewController:activitieVC animated:YES];
    [activitieVC release];
    
}
#pragma mark - 加载数据
- (void)getUpdate
{
    
    // 让菊花旋转起来
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"努力加载中...";
    
    NSString *cityid = [[NSUserDefaults standardUserDefaults] objectForKey:@"cityid"];
    NSString *url = [NSString stringWithFormat:@"http://community.api.jtwsm.cn/Story_StoryListByCity.do?appkey=aef6fd10-bb02-4f44-885e-927ed7d64da4&cityCode=%@&source=%d&version=%d", cityid, URL_SOURCE, URL_VERSION];

    
    [AppTools getAFNet:url block:^(id result) {
    
        
        [self.recordArr removeAllObjects];
        
        NSMutableDictionary *bigDic = result;
        
        NSMutableArray *userList = [[bigDic objectForKey:@"obj"] objectForKey:@"userList"];
        NSMutableArray *storyList = [[bigDic objectForKey:@"obj"] objectForKey:@"storyList"];
 
        for (NSMutableDictionary *storyDic in storyList) {
            
            NSString *sendUserid = [NSString stringWithFormat:@"%@", [storyDic objectForKey:@"sendUserid"]];
            
            RecordMod *recordMod = [[RecordMod alloc] init];
            [recordMod setValuesForKeysWithDictionary:storyDic];
            
            for (NSMutableDictionary *dic in userList) {
                
                NSString *userId = [NSString stringWithFormat:@"%@", [dic objectForKey:@"userId"]];
                if ([userId isEqualToString:sendUserid]) {
                    [recordMod setValuesForKeysWithDictionary:dic];
                    [self.recordArr addObject:recordMod];
                    [recordMod release];
                }
                
            }
        }
        
//        for (RecordMod *mod in self.recordArr) {
//            NSLog(@"%@ -- %ld", mod.userName, mod.userId);
//        }
        
        [self.recordTable reloadData];
        
        [self.recordTable.header endRefreshing];
                // 让菊花停止旋转
        [self.hub hide:YES];
    } errorBlock:^(NSError *error) {
        
        [self NetworkError:^{
        }];
        
        //结束下拉上拉刷新
        [self.recordTable.header endRefreshing];
        [self.recordTable.footer endRefreshing];
        [self.hub hide:YES];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
